/*
 * Copyright (c) 2009-2013 EvTech Project
 * All rights reserved.
 * 
 * This file is part of LModeller.
 *
 * LModeller is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 *
 * LModeller is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LModeller.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package fi.honkalampisaatio.lmodeller.blocks;

import org.lwjgl.util.vector.Vector3f;


public class Block {
	public enum COLOR {BLACK, BLUE, GREEN, LIGHTGREEN, RED, YELLOW, WHITE, ORANGE, TRANSBLUE};
	private COLOR color = COLOR.WHITE;

	public enum TYPE {BASIC2x2, BASIC4x2, PLATE4x2};
	private TYPE type = TYPE.BASIC2x2;
	
	Vector3f location = new Vector3f();
	int rotation = 0;
	
	public void setType(TYPE type) {
		this.type = type;
	}
	
	public boolean setBlockByLdrFile(String line)
	{
		//Parsi rivi
		// 1 14 -100 0 0 1 0 0 0 1 0 0 0 1 3001.DAT
		String values[] = line.split(" ");
		try {
			if(values.length < 15)
			{
				return false;
			}
			if(!values[0].equalsIgnoreCase("1"))
			{
				return false;
			}
			//TYPE

			if(values[14].equalsIgnoreCase("3003.DAT"))
			{
				this.type = TYPE.BASIC2x2;
			}
			else if(values[14].equalsIgnoreCase("3001.DAT"))
			{
				this.type = TYPE.BASIC4x2;
			}
			else if(values[14].equalsIgnoreCase("3020.DAT"))
			{
				this.type = TYPE.PLATE4x2;
			}
			else
			{
				return false;
			}
			//COLOR
			int clr = Integer.parseInt(values[1]);
			switch(clr)
			{
			case 0:
				color = COLOR.BLACK;
				break;
			case 1:
				color = COLOR.BLUE;
				break;
			case 2:
				color = COLOR.GREEN;
				break;
			case 4:
				color = COLOR.RED;
				break;
			case 10:
				color = COLOR.LIGHTGREEN;
				break;
			case 14:
				color = COLOR.YELLOW;
				break;
			case 15:
				color = COLOR.WHITE;
				break;
			case 25:
				color = COLOR.ORANGE;
				break;
			case 33:
				color = COLOR.TRANSBLUE;
				break;
			default:
				return false;
			}
			location = new Vector3f(Float.parseFloat(values[2]),
					Float.parseFloat(values[3]),
					Float.parseFloat(values[4]));

			if(Integer.parseInt(values[5])==1 || Integer.parseInt(values[5])==-1)
			{
				rotation = 0;
			}
			else
			{
				rotation = 90;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

		return true;
	}

	public void setColor(COLOR color) {
		this.color = color;
	}

	public int getLenght() {
		switch(type)
		{
		case BASIC2x2: return 40;
		case BASIC4x2: return 80;
		case PLATE4x2: return 80;
		}
		return 0;
	}

	public int getWidth () {
		switch(type)
		{
		case BASIC2x2: return 40;
		case BASIC4x2: return 40;
		case PLATE4x2: return 40;
		}
		return 0;
	}

	public int getHeight() {
		switch(type)
		{
		case BASIC2x2: return 24;
		case BASIC4x2: return 24;
		case PLATE4x2: return 12;
		}
		return 0;
	}

	public void setLocation(Vector3f location) {
		this.location = location;
		
	}

	public void setRotation(int rotation) {
		this.rotation= rotation;
		
	}

	public TYPE getType() {
		return type;
	}

	public float getX() {
		return location.x;
	}

	public float getY() {
		return location.y;
	}

	public float getZ() {
		return location.z;
	}

	public float getRotate() {
		return rotation;
	}

	public COLOR getColor() {
		return color;
	}

	public String toString()
	{
		return "Type: " + type.toString() + ", color: " + color.toString() +
			", location: " + location.toString() + ", rotation: "+ rotation;
	}


	public boolean compare(Block b)
	{
		if(b.getType()==this.getType() && b.getColor()==this.getColor())
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	public void setY(float y) {
		location.y = y;
	}
}
